<template>
  <!-- 空间图片大小分析 -->
  <div id="space-category-analyze">
    <a-card title="空间图片大小分析">
      <v-chart :option="option" style="height: 320px; max-width: 100%" :loading="loading"></v-chart>
    </a-card>
  </div>
</template>

<script setup lang="ts">
import { ref, withDefaults, defineProps, onMounted, computed } from 'vue'
import {
  getSpaceSizeAnalyzeUsingPost,
} from '@/request/api/spaceAnalyzeController.ts'
import { message } from 'ant-design-vue'

interface Props {
  queryAll?: boolean
  queryPublic?: boolean
  spaceId?: number
}

const props = withDefaults(defineProps<Props>(), {
  queryAll: false,
  queryPublic: false,
})

const dataList = ref<API.SpaceSizeAnalyzeResponse[]>([])
const loading = ref(false)

const fetchData = async () => {
  loading.value = true
  const { data: res } = await getSpaceSizeAnalyzeUsingPost({
    queryAll: props.queryAll,
    queryPublic: props.queryPublic,
    spaceId: props.spaceId,
  })
  if (res.code === 0 && res.data) {
    dataList.value = res.data
  } else {
    message.error('获取数据失败,' + res.message)
  }
  loading.value = false
}
const option = computed(() => {
  const pieData = dataList.value.map((item) => ({
    name: item.sizeRange,
    value: item.count,
  }))

  return {
    tooltip: {
      trigger: 'item',
      formatter: '{a} <br/>{b}: {c} ({d}%)',
    },
    legend: {
      top: 'bottom',
    },
    series: [
      {
        name: '图片大小',
        type: 'pie',
        radius: '50%',
        data: pieData,
      },
    ],
  }
})

onMounted(() => {
  fetchData()
})
</script>

<style lang="less" scoped></style>
